package com.clevertech.tests;

import com.clevertech.tests.Utils.ClevertechSeleniumUtils;

import java.util.Random;

public class TestCaseOne extends ClevertechSeleniumUtils {

    public void testNew() throws Exception {
        selenium.open("http://binf.clevertech.biz");
        selenium.windowMaximize();
        selenium.type("//*[@id='login']", "DannyC");
        selenium.type("//*[@id='password']", "Usa123");
        Thread.sleep(2000);
        selenium.click("//a[@href='javascript:document.loginForm.submit();']");
        selenium.waitForPageToLoad("600000");
        Thread.sleep(2000);
        selenium.click("//*[@id='nav_check_request']");
        selenium.waitForPageToLoad("600000");
        Thread.sleep(2000);
        selenium.click("//a[@href='check-request.cfm']");
        selenium.waitForPageToLoad("600000");
        String url = selenium.getLocation() + "?testcase=1";
        selenium.open(url);
         Thread.sleep(1000);
        selenium.typeKeys("//*[@id='vendor_mainInput']", "Gal");
         waitForElement("//*[@id='vendor_preSelected']//*[contains(@id,'anonymous_element')]", 600000);
        Thread.sleep(1000);
        selenium.click("//*[@id='vendor_preSelected']//*[contains(@id,'anonymous_element')]");
        Thread.sleep(2000);
        selenium.type("//*[@id='invoice_number']", generateInvoiceNumber());
         Thread.sleep(1000);
        selenium.type("//*[@id='amount']", "1000");
        Thread.sleep(1000);
        selenium.type("//*[@id='invoicedate']", "06/01/2010");
         Thread.sleep(1000);
        selenium.type("//*[@id='dateneeded']", "06/01/2010");
        Thread.sleep(1000);
        selenium.type("//*[@id='discofexp']", "Any description");
         Thread.sleep(1000);
        selenium.typeKeys("selectedGLOption1_mainInput", "a");
        waitForElement("//*[@id='selectedGLOption1_preSelected']//*[contains(@id,'anonymous_element')]", 600000);
        Thread.sleep(1000);
        selenium.click("//*[@id='selectedGLOption1_preSelected']//*[contains(@id,'anonymous_element')]");
        Thread.sleep(2000);

        selenium.typeKeys("selectedProjectOption1_mainInput", "Strategic");
        waitForElement("//*[@id='selectedProjectOption1_preSelected']//*[contains(@id,'anonymous_element')]", 600000);
        Thread.sleep(1000);
        selenium.click("//*[@id='selectedProjectOption1_preSelected']//*[contains(@id,'anonymous_element')]");
        Thread.sleep(2000);

        selenium.typeKeys("selectedDeptOption1_mainInput", "Local");
        waitForElement("//*[@id='selectedDeptOption1_preSelected']//*[contains(@id,'anonymous_element')]", 600000);
        Thread.sleep(1000);
        selenium.click("//*[@id='selectedDeptOption1_preSelected']//*[contains(@id,'anonymous_element')]");
        Thread.sleep(2000);

        selenium.typeKeys("selectedTagsOption1_mainInput", "Profe");
        waitForElement("//*[@id='selectedTagsOption1_preSelected']//*[contains(@id,'anonymous_element')]", 60000);
        Thread.sleep(1000);
        selenium.click("//*[@id='selectedTagsOption1_preSelected']//*[contains(@id,'anonymous_element')]");
        Thread.sleep(2000);

        selenium.typeKeys("selectedProgOption1_mainInput", "A");
        waitForElement("//*[@id='selectedProgOption1_preSelected']//*[contains(@id,'anonymous_element')]", 600000);
        Thread.sleep(1000);
        selenium.click("//*[@id='selectedProgOption1_preSelected']//*[contains(@id,'anonymous_element')]");
        Thread.sleep(2000);

        fileUpload();

        selenium.click("//*[@id='SubmitCheckRequestButton']");
        selenium.waitForPageToLoad("60000");
        String location = selenium.getLocation();
        if(!location.contains("/check-req-list.cfm"))
        {
            selenium.logAssertion("Check system redirected to","check-req-list.cfm",location);
        }


        Thread.sleep(20000);


      //   assertTrue(location.contains("/check-req-list.cfm?msg=Your Check Request contained a duplicate Invoice Number"));
//        selenium.click("//*[@href='logout.cfm']");
//        selenium.open("http://binf.clevertech.biz/logout.cfm");
//        selenium.waitForPageToLoad(DEFAULT_TIMEOUTL);
    }

    private void fileUpload() throws InterruptedException {
        String curDir = System.getProperty("user.dir"); 
//        selenium.selectFrame("testCaseFrame");
        selenium.type("//*[@id='filedata']", curDir+"\\trunk\\resources\\test.jpg");
//        selenium.selectFrame("relative=up");
        selenium.click("sendFile");
        selenium.waitForPageToLoad("6000000");
//        selenium.selectFrame("relative=up");
        waitForElement("//div[@class='imgCrop_clickArea']", 600000);
        selenium.click("//div[@class='imgCrop_clickArea']");
        Thread.sleep(2000);
        selenium.type("testimage_x1", "0");
        selenium.type("testimage_y1", "0");
        selenium.type("testimage_x2", "200");
        selenium.type("testimage_y2", "200");
        selenium.type("testimage_width", "200");
        selenium.type("testimage_height", "200");
        
        waitForElement("//a[contains(@onclick,'Crop Receipt')]", 600000);
        selenium.click("//a[contains(@onclick,'Crop Receipt')]");
        selenium.waitForPageToLoad("6000000");
        Thread.sleep(2000);
        waitForElement("//*[contains(@onclick,'Save Cropped Image')]", 600000);
        selenium.click("//*[contains(@onclick,'Save Cropped Image')]");
        selenium.waitForPageToLoad("6000000");
    }

    private void waitForElement(String xpath, int timeoutMiliSeconds) {
        boolean isNotAvailable = true;
        while (isNotAvailable && timeoutMiliSeconds > 0) {
            if (selenium.isElementPresent(xpath)) {
                isNotAvailable = false;
            }
            try {
                Thread.sleep(200);
                timeoutMiliSeconds = timeoutMiliSeconds - 200;
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }

    private String generateInvoiceNumber() {
        Random generator = new Random();
        int invoice = generator.nextInt(126548798);
        return Integer.toString(invoice);
    }

}
